<?php
class User extends AppModel {

	var $name = 'User';
	var $recursive = 0;

	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $hasOne_active = array(
		'Mailaddress' => array(
			'className' => 'Mailaddress',
			'foreignKey' => 'user_id',
			'dependent' => true,
			'conditions' => array('Mailaddress.status' => 'active'),
		),
	);
	var $hasMany_active = array(
		'Profile' => array(
			'className' => 'Profile',
			'foreignKey' => 'user_id',
			'dependent' => true,
			'conditions' => array('Profile.status' => 'active'),
		),
		'Post' => array(
			'className' => 'Post',
			'foreignKey' => 'user_id',
			'dependent' => true,
			'conditions' => array('Post.status' => 'active'),
		),
		'Friend' => array(
			'className' => 'Friend',
			'foreignKey' => 'user_id',
			'dependent' => true,
			'conditions' => array('Friend.status' => 'active'),
		),
	);
	var $hasMany_full = array(
		'Comment' => array(
			'className' => 'Comment',
			'foreignKey' => 'user_id',
			'dependent' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Friend' => array(
			'className' => 'Friend',
			'foreignKey' => 'user_id',
			'dependent' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Mailaddress' => array(
			'className' => 'Mailaddress',
			'foreignKey' => 'user_id',
			'dependent' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Picture' => array(
			'className' => 'Picture',
			'foreignKey' => 'user_id',
			'dependent' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Post' => array(
			'className' => 'Post',
			'foreignKey' => 'user_id',
			'dependent' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Profile' => array(
			'className' => 'Profile',
			'foreignKey' => 'user_id',
			'dependent' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
	);
	var $belongsTo = array(
		'Icon' => array(
			'className' => 'Icon',
			'foreignKey' => 'icon_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
	);
	//バリデート
	var $validate = array(
		'status' => array(
			'rule' => array('isInArray', array('notActive', 'active', 'resign')),
		),
		'controlprofile' => array(
			'rule' => array('isInArray', array('onlyMe', 'friend', 'friendFriend', 'all')),
		),
		'controlpost' => array(
			'rule' => array('isInArray', array('onlyMe', 'friend', 'friendFriend', 'all')),
		),
		'ip' => array(
			'rule' => array('ip'),
		),
		'icon_id' => array(
			'rule' => array('isPlusNumber'),
		),
		'password' => array(
			VALID_NOT_EMPTY,
		),
		'passraw' => array(
			VALID_NOT_EMPTY,
			'kaigyo' => array(
				'rule' => array('kaigyoLessThanN', 0),
				'message' => 'パスワードは改行できません',
			),
			'min' => array(
				'rule' => array('minLength', 6),
				'message' => 'パスワードは6文字以上必要です',
			),
		),
		'nickname' => array(
			VALID_NOT_EMPTY,
			'length' => array(
				'rule' =>array('between', 4, 20),
				'message' => 'ニックネームは4-20文字までです',
			),
			'kaigyo' => array(
				'rule' => array('kaigyoLessThanN', 0),
				'message' => 'ニックネームは改行できません',
			),
		),
	);
	
	//登録時のバリデート
	var $validate_register = array(
		'status' => array(
			'rule' => array('isInArray', array('notActive', 'active', 'resign')),
		),
		'ip' => array(
			'rule' => array('ip'),
		),
		'icon_id' => array(
			'rule' => array('isPlusNumber'),
		),
		'controlprofile' => array(
			'rule' => array('isInArray', array('onlyMe', 'friend', 'friendFriend', 'all')),
		),
		'controlpost' => array(
			'rule' => array('isInArray', array('onlyMe', 'friend', 'friendFriend', 'all')),
		),
	);
	
	function getOtherWithProfiles($myID, $otherID)
	{
		/*
		array('onlyMe', 'friend', 'friendFriend', 'all')
		*/
		
		App::import('Model', 'Friend');
		$friend = new Friend();
		if($friend->isUser($myID, $otherID)){
			$conditions = array(
				'AND' => array(
					'Profile.status' => 'active',
					'Profile.control' => array(
						'onlyMe',
						'friend',
						'friendFriend',
						'all',
					),
				),
			);
		}elseif($friend->isUserFriend($myID, $otherID)){
			$conditions = array(
				'AND' => array(
					'Profile.status' => 'active',
					'Profile.control' => array(
						'friend',
						'friendFriend',
						'all',
					),
				),
			);
		}elseif($friend->isUserFriendFriend($myID, $otherID)){
			$conditions = array(
				'AND' => array(
					'Profile.status' => 'active',
					'Profile.control' => array(
						'friendFriend',
						'all',
					),
				),
			);
		}else{
			$conditions = array(
				'AND' => array(
					'Profile.status' => 'active',
					'Profile.control' => array(
						'all',
					),
				),
			);
		}
		
		$bindusers = array(
			'hasMany' => array(
				'Profile' => array(
					'className' => 'Profile',
					'foreignKey' => 'user_id',
					'dependent' => true,
					'conditions' => $conditions,
				),
			),
		);
		
		$this->bindModel($bindusers);
		$this->recursive = 1;
		return $this->findByIdAndStatus($otherID, 'active');
	}
	
	
	function changeNickname($newNickname)
	{
		$data = array();
		$data['nickname'] = $newNickname;
		$data['ip'] = $this->RHgetClientIP();
		$data['modified'] = null;
		
		return $this->save(array($this->name => $data), true, array('ip', 'nickname', 'modified'));
	}
	
	function changePassword($newPassword)
	{
		$data = array();
		$data['password'] = md5(Configure::read('salt.password').$newPassword);
		$data['passraw'] = $newPassword;
		$data['ip'] = $this->RHgetClientIP();
		$data['modified'] = null;
		
		return $this->save(array($this->name => $data), true, array('ip', 'password', 'passraw', 'modified'));
	}
	
	
	function checkActivate()
	{
		return in_array($this->data[$this->name]['status'], array('notActive', 'active'));
	}
	
	function changePostControl($post_control)
	{
		$data = array();
		$data['controlpost'] = $post_control;
		$data['ip'] = $this->RHgetClientIP();
		$data['modified'] = null;
		
		return $this->save(array($this->name => $data), true, array('ip', 'controlpost', 'modified'));
	}
	
	function changeProfileControl($profile_control)
	{
		$data = array();
		$data['controlprofile'] = $profile_control;
		$data['ip'] = $this->RHgetClientIP();
		$data['modified'] = null;
		
		return $this->save(array($this->name => $data), true, array('ip', 'controlprofile', 'modified'));
	}
	
	function changeIcon($iconID)
	{
		$data = array();
		//iconIDが存在するかどうか
		App::import('Model', 'Icon');
		$icon = new Icon();
		$data['icon_id'] = $icon->field('id', array('id' => '= '.$iconID));
		if(!$data['icon_id']){
			$this->invalidate('notexist', 'ないよ');
			return false;
		}
		
		$data['ip'] = $this->RHgetClientIP();
		$data['modified'] = null;
		
		return $this->save(array($this->name => $data), true, array('ip', 'icon_id', 'modified'));
	}
	
	function activate($nickname, $password)
	{
		$data = array();
		$data['nickname'] = $nickname;
		$data['password'] = md5(Configure::read('salt.password').$password);
		$data['passraw'] = $password;
		$data['status'] = 'active';
		$data['ip'] = $this->RHgetClientIP();
		$data['modified'] = null;
		
		return $this->save(array($this->name => $data), true, array('nickname', 'password', 'status', 'ip', 'modified'));
	}
	
	function register()
	{
		$data = array();
		$data['status'] = 'notActive';
		$data['controlprofile'] = 'onlyMe';
		$data['controlpost'] = 'onlyMe';
		$data['ip'] = $this->RHgetClientIP();
		App::import('Model', 'Icon');
		$icon = new Icon();
		$data['icon_id'] = $icon->getRandomNum();
		if(!$data['icon_id']){return false;}
		
		$this->validate = $this->validate_register;
		return $this->save(array($this->name => $data));
	}
	
	function isAuth($password)
	{
		$hash = $this->data['User']['password'];
		return (md5(Configure::read('salt.password').$password) === $hash);
	}
	
	function getUserID()
	{
		return (int)$this->getLastInsertID();
	}
	
	function isActivatableStatus($data)
	{
		debug($data);
		return in_array($data[$this->name]['status'], array('notActive', 'active'));
	}

}
?>